<?php

/**
 * @author kevin
 * @date 2016-04-23 10:00:00
 * @desc 购物指南
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
$version = '1.0';   //版本号

if ($_POST['api_version'] != $version)      //网店的接口版本低
{
	api_err('0x008', 'a low version api');
}
//指南列表
if($_POST['st'] == 'list'){
	$page = isset($_POST['page'])?intval(abs($_POST['page'])):1;
	$size = abs(intval($_POST['size']));
	$size = ($size > 0 && $size < SIZE_MAX)?$size:SIZE_VALUE;
	$count = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " .$ecs->table('guide').
                                " WHERE is_open = 1 ORDER BY guide_id DESC");
	$pager = get_pager('user.php', array('act' => 'collection_list'), $count, $page, $size);
	$data['list'] = get_guide_list($page, $size);
	$data['page'] = get_app_pager($pager);
}
//指南详情
if($_POST['st'] == 'detail'){
	$guide_id = isset($_POST['guide_id'])?intval(abs($_POST['guide_id'])):0;
	if(empty($guide_id)){
		api_err('0x008', 'param guide_id is error');
	}
	$data = get_guide_detail($guide_id);
}
//st参数错误
if(!isset($data)){
	api_err('0x008');
}
//返回数据
data_back($data, '', RETURN_TYPE);

//购物指南列表
function get_guide_list($page, $size){
	$return = array();
	if(empty($page) || empty($size)){
		return $return;
	}
	$sql = "SELECT * FROM " .$GLOBALS['ecs']->table('guide')." WHERE is_open = 1 ORDER BY guide_id DESC limit ".($size*($page-1)).",".$size;
	$list = $GLOBALS['db']->getAll($sql);
	foreach($list as $value){
		//略缩图处理
		$list = json_decode($value['thumb'],true);
		$thumb = array();
		if(!empty($list)){
			foreach($list as $v){
				$thumb[] = get_image_pathv1('',$v['img_url']);
			}
		}
        $keywords = '';
        if(!empty($value['keywords'])){
            $keywords = "#{$value['keywords']}#";
        }
		$return[] = array(
			'guide_id'=>$value['guide_id'],
			'title'=>$value['title'],
            'keywords'=>$keywords,
			'description'=>delete_html($value['description']),
			'thumb'=>$thumb
		);
	}
	return $return;
}
//购物指南详情
function get_guide_detail($guide_id){
	$return = array();
	if(empty($guide_id)){
		return $return;
	}
	$sql = "select * from ".$GLOBALS['ecs']->table('guide')." where is_open=1 and guide_id = ".$guide_id;
	$row = $GLOBALS['db']->getRow($sql);
	if(!empty($row)){
		//文章内容处理
		$list = json_decode($row['content'],true);
		$content = array();
		if(!empty($list)){
			foreach($list as $value){
				$content[] = get_image_pathv1('',$value['img_url']);
			}
		}
		//相关商品处理
		$list = get_guide_goods($guide_id);
		$goods = array();
		if(!empty($list)){
			foreach($list as $key=>$value){
				if ($value['promote_price'] > 0)
				{
					$promote_price = bargain_price($value['promote_price'], $value['promote_start_date'], $value['promote_end_date']);
				}
				else
				{
					$promote_price = 0;
				}
				$value['goods_thumb'] = get_image_pathv1('',$value['goods_thumb']);
				$value['watermark_img'] = get_water_img($value['goods_id'],$value['is_new'],$promote_price);
				$goods[] = $value;
			}
		}
        $keywords = '';
        if(!empty($row['keywords'])){
            $keywords = "#{$row['keywords']}#";
        }
		$return = array(
			'guide_id'=>$row['guide_id'],
			'title'=>$row['title'],
			'author'=>$row['author'],
			'keywords'=>$keywords,
			'add_time'=>date("Y-m-d H:i:s", $row['add_time']),
			'content'=>$content,
			'goods'=>$goods
		);
	}
	return $return;
}
//获取相关联商品
function get_guide_goods($guide_id)
{
    $list = array();
    $sql  = 'SELECT g.goods_id, g.goods_name,g.goods_number,g.goods_thumb,g.market_price,g.shop_price,g.promote_price,g.is_new,promote_start_date, promote_end_date'.
            ' FROM ' . $GLOBALS['ecs']->table('goods_guide') . ' AS ga'.
            ' LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = ga.goods_id'.
            " WHERE ga.guide_id = '$guide_id'";
    $list = $GLOBALS['db']->getAll($sql);

    return $list;
}
function delete_html($str){
    $str = trim($str);
    $str = strip_tags($str,"");
    $str = str_replace("\t","",$str);
    $str = str_replace("\r\n","",$str);
    $str = str_replace("\r","",$str);
    $str = str_replace("\n","",$str);
    $str = str_replace("&nbsp;","",$str);
    $str = str_replace(" "," ",$str);
    return trim($str);
}
